package cn.edu.scnu.semart.goods.service.Impl;

import cn.edu.scnu.semart.model.goods.Goods;
import cn.edu.scnu.semart.model.goods.GoodsAudit;
import cn.edu.scnu.semart.goods.mapper.GoodsAuditMapper;
import cn.edu.scnu.semart.goods.service.GoodsAuditService;
import cn.edu.scnu.semart.goods.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 商品审核Service实现类
 */
@Service
public class GoodsAuditServiceImpl extends ServiceImpl<GoodsAuditMapper, GoodsAudit> implements GoodsAuditService {

    @Autowired
    private GoodsService goodsService;

    @Override
    @Transactional
    public boolean submitForAudit(Integer goodsId) {
        // 检查商品是否存在
        Goods goods = goodsService.getById(goodsId);
        if (goods == null) {
            return false;
        }

        // 更新商品状态为待审核
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", goodsId)
                    .set("status", 3)  // 待审核
                    .set("audit_status", 0);  // 待审核
        boolean success = goodsService.update(updateWrapper);

        if (success) {
            // 创建审核记录
            GoodsAudit audit = new GoodsAudit();
            audit.setGoodsId(goodsId);
            audit.setAuditStatus(0);  // 待审核
            return save(audit);
        }

        return false;
    }

    @Override
    @Transactional
    public boolean approve(Integer goodsId, Integer auditorId, String auditorName, String remark) {
        // 更新商品状态为审核通过
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", goodsId)
                    .set("status", 1)  // 上架
                    .set("audit_status", 1)  // 审核通过
                    .set("auditor_id", auditorId)
                    .set("auditor_name", auditorName)
                    .set("audit_time", LocalDateTime.now())
                    .set("audit_remark", remark);
        boolean success = goodsService.update(updateWrapper);

        if (success) {
            // 更新审核记录
            UpdateWrapper<GoodsAudit> auditUpdateWrapper = new UpdateWrapper<>();
            auditUpdateWrapper.eq("goods_id", goodsId)
                            .eq("audit_status", 0)  // 待审核状态
                            .set("audit_status", 1)  // 审核通过
                            .set("auditor_id", auditorId)
                            .set("auditor_name", auditorName)
                            .set("audit_time", LocalDateTime.now())
                            .set("audit_remark", remark);
            return update(auditUpdateWrapper);
        }

        return false;
    }

    @Override
    @Transactional
    public boolean reject(Integer goodsId, Integer auditorId, String auditorName, String rejectReason) {
        // 更新商品状态为审核拒绝
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", goodsId)
                    .set("status", 4)  // 审核拒绝
                    .set("audit_status", 2)  // 审核拒绝
                    .set("auditor_id", auditorId)
                    .set("auditor_name", auditorName)
                    .set("audit_time", LocalDateTime.now())
                    .set("reject_reason", rejectReason);
        boolean success = goodsService.update(updateWrapper);

        if (success) {
            // 更新审核记录
            UpdateWrapper<GoodsAudit> auditUpdateWrapper = new UpdateWrapper<>();
            auditUpdateWrapper.eq("goods_id", goodsId)
                            .eq("audit_status", 0)  // 待审核状态
                            .set("audit_status", 2)  // 审核拒绝
                            .set("auditor_id", auditorId)
                            .set("auditor_name", auditorName)
                            .set("audit_time", LocalDateTime.now())
                            .set("reject_reason", rejectReason);
            return update(auditUpdateWrapper);
        }

        return false;
    }

    @Override
    public IPage<GoodsAudit> getPendingAuditList(Page<GoodsAudit> page) {
        QueryWrapper<GoodsAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit_status", 0)  // 待审核
                   .orderByDesc("create_time");
        return page(page, queryWrapper);
    }

    @Override
    public IPage<GoodsAudit> getAuditHistory(Integer goodsId, Page<GoodsAudit> page) {
        QueryWrapper<GoodsAudit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id", goodsId)
                   .orderByDesc("audit_time");
        return page(page, queryWrapper);
    }

    @Override
    public AuditStatistics getAuditStatistics() {
        AuditStatistics statistics = new AuditStatistics();

        // 统计待审核数量
        QueryWrapper<GoodsAudit> pendingQuery = new QueryWrapper<>();
        pendingQuery.eq("audit_status", 0);
        Integer pendingCount = Math.toIntExact(count(pendingQuery));
        statistics.setPendingCount((int) pendingCount);

        // 统计审核通过数量
        QueryWrapper<GoodsAudit> approvedQuery = new QueryWrapper<>();
        approvedQuery.eq("audit_status", 1);
        Integer approvedCount = Math.toIntExact(count(approvedQuery));
        statistics.setApprovedCount((int) approvedCount);

        // 统计审核拒绝数量
        QueryWrapper<GoodsAudit> rejectedQuery = new QueryWrapper<>();
        rejectedQuery.eq("audit_status", 2);
        Integer rejectedCount = Math.toIntExact(count(rejectedQuery));
        statistics.setRejectedCount((int) rejectedCount);

        // 总数量
        statistics.setTotalCount((int) (pendingCount + approvedCount + rejectedCount));

        return statistics;
    }
} 